package com.sam.utils;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Properties;

import org.apache.struts2.ServletActionContext;

import com.google.api.client.auth.oauth.OAuthHmacSigner;
import com.google.api.client.auth.oauth.OAuthParameters;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.jackson.JacksonFactory;
import com.google.api.client.util.GenericData;
import com.google.api.services.calendar.Calendar;
import com.google.api.services.calendar.Calendar.Builder;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sam.entities.Admisor;
import com.sam.entities.Admisores;
import com.sam.entities.Afiliado;

public class Utils {
	
	private static Properties prop;
	public static final String GOOGLE_KEY = "AIzaSyBJwUVfurVheN7_oJy4vBOGNKqqAIZuj-k";
	private static final List<String> colors = new ArrayList<String>(Arrays.asList("2952A3","A32929","865A5A","0D7813","AB8B00","BE6D00","8D6F47","5229A3","2F6213","B1365F","42104A"));
	
	public static Calendar getCalendarServices()
	{
		HttpTransport httpTransport = new NetHttpTransport();
		JacksonFactory jsonFactory = new JacksonFactory();

		OAuthHmacSigner signer = new OAuthHmacSigner();
		signer.clientSharedSecret = "O7Mt34OnIGAHrIZTd1MFTbNf";
		OAuthParameters oauthParameters = new OAuthParameters();
		oauthParameters.version = "2.0";
		oauthParameters.consumerKey = "mycrazymula.com.ar";
		oauthParameters.signer = signer;
		//return Calendar.builder(httpTransport, jsonFactory).setApplicationName("calendardemoosde").setHttpRequestInitializer(oauthParameters).build();
		Builder builder = new Calendar.Builder(httpTransport, jsonFactory, oauthParameters);
		return builder.setApplicationName("calendardemoosde").build();
	}
	
	public static void setGoogleKey(GenericData genericData) {
		genericData.getUnknownKeys().put("key", Utils.GOOGLE_KEY);
	}
	
	public static String getListColors(int index) {
		return colors.get(index);
	}

	public static void setRequestorId(GenericData genericData, String requestor) {
		genericData.getUnknownKeys().put("xoauth_requestor_id", requestor);
	}
	

	public static Afiliado obtenerAfiliado(String numeroAfiliado){
		try {
			String uploadDir = ServletActionContext.getServletContext().getRealPath("/files/" + getProperty("afiliadosFile"));
			BufferedReader br = new BufferedReader(new FileReader(uploadDir));
			Gson gson = new Gson();
			
			Type type = new TypeToken<Collection<Afiliado>>() {}.getType();
			Collection<Afiliado> afiliados = gson.fromJson(br, type);
			
			for (Afiliado afiliado : afiliados) {
				if(afiliado.getNumeroAfiliado().equals(numeroAfiliado)){
					return afiliado;
				}
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static Admisor obtenerAdmisor(String mailAdmisor) {
		try {
			String uploadDir = ServletActionContext.getServletContext()
					.getRealPath("/files/" + getProperty("admisoresFile"));
			BufferedReader br = new BufferedReader(new FileReader(uploadDir));
			Gson gson = new Gson();
			Admisores obj = gson.fromJson(br, Admisores.class);
			List<Admisor> admisores = obj.getAdmisor();
			for (Admisor admisor : admisores) {
				if (mailAdmisor.equals(admisor.getEmail())) {
					return admisor;
				}
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static String getProperty(String key) {
		if (prop == null) {
			try {
				prop = new Properties();
				prop.load(Utils.class.getResourceAsStream("/config.properties"));
			} catch (IOException ex) {
				ex.printStackTrace();
			}
		}
		return prop.getProperty(key);
	}
	
}
